<?php include '../../authorization_check.php' ?>
<?php include '../../checkSecretaryOnly.php' ?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-9">
<title>Open New Section</title>
<SCRIPT TYPE="text/javascript" src= "../../commonutils/util/util.js"> </SCRIPT>
</head>
<style type="text/css">
  th {text-align: right}  
</style>

<SCRIPT TYPE="text/javascript" >
 function controlFields(){
   var myForm = document.form1;
   
   if (radioControl(myForm.courseGroup) && radioControl(myForm.instructorGroup) && radioControl(myForm.dayGroup))
   {
		return true;
   }
   else{
      alert("Course, instructor and day fields must be selected.");
      return false;
   }
    
 }
 

</SCRIPT>

<body>
<?php include 'top.html' ?>

<?php include 'notifier.php' ?>


  
<FORM NAME ="form1" METHOD ="POST" ACTION = "OpenSection.php">
<table border=1 cellpadding=5>
<tr>

<th style="text-align:center">COURSE CODE </th>
<th style="text-align:center">COURSE NAME </th>
<th style="text-align:center">NUMBER OF OPEN SECTIONS </th>
</tr>

<p>
<?php
   require_once '../../commonutils/db/DBConnectionManager.php';

$numerrors = 0;   
$roomError = 0;
if (isset($_POST['btnSubmit']))
{
	$courseCode = $_POST['courseGroup']; 
	$sqlquery = " select count(*) + 1 as cnt from section where courseCode = ".$courseCode;
	//echo $sqlquery."<br>";
	$result = DBConnectionManager::query($sqlquery);
	$row = mysql_fetch_assoc($result);
	$sectionNumber = $row['cnt'];
	if($sectionNumber < 10)
	  $sectionID = $courseCode."0".$sectionNumber;
	else
	  $sectionID = $courseCode.$sectionNumber;
	  
	$instructorID = $_POST['instructorGroup'];
	$startTime = $_POST['sho'].":40";
	$room = strtoupper($_POST['room']);	
	$hours = $_POST['hours'];
	$lectureDay = $_POST['dayGroup'];
	
	$shour = $_POST['sho'];
	$ehour = $shour + $hours - 1;
	$endTime = $ehour.":40";
	
	
	
	$sqlquery = " select count(*) as cnt".
				" from section s ".
				" where s.lectureDay = ".$lectureDay." and ".
					" s.roomCode = '".$room."' and ".
					" ( s.beginTime <= '".$startTime."' and ".
					"   timediff(s.begintime, Time(concat('-',s.hours,':00'))) >= '".$startTime."' or ".
					" ( s.beginTime <= '".$endTime."' and ".
					"   timediff(s.begintime, Time(concat('-',s.hours,':00'))) >= '".$endTime."' ))";
	// echo $sqlquery."<br>";
	$roomError = 1;
	
	$result = DBConnectionManager::query($sqlquery);
	$row = mysql_fetch_assoc($result);
	$cnt = $row['cnt'];
	if($cnt > 0){
		$numerrors = $numerrors + 1;
		echo "<p><font color=red><b>* There is another lecture in the same room in the specified time period.</b> </font></p>";
		echo "<p><a href=ViewSections.php> View sections. </a></p>";
	}
	  
	  
	if($numerrors == 0){  
	
		$sqlquery = " insert into section(semesterID, sectionID, courseCode, sectionNumber, instructorID, ".
					" roomCode, beginTime, hours, lectureday) values"
					."(".$semesterID.",".$sectionID.", ".$courseCode.", ".$sectionNumber.", ".$instructorID.", '".
					$room."', '".$startTime."',".$hours.",".$lectureDay.")" ;
		echo "<p><font color=blue><b>You have successfully opened a new section.</b></font></p>";
		echo "<p><a href=ViewSections.php> View sections. </a></p>";
		//echo $sqlquery."<br>";
		DBConnectionManager::query($sqlquery);
	
	}

}

?>

<?php

	$sqlquery = " select course.courseCode, course.courseName, count(section.sectionID) as cnt ".
				" from course left outer join section on course.courseCode = section.courseCode ".
				" where course.programCode = ".$programCode.
				" group by course.courseCode, course.courseName ";
	
	// echo $sqlquery;  
	
	$result = DBConnectionManager::query($sqlquery);
	while($row = mysql_fetch_assoc($result)){
		echo "<tr>";
		echo "<td> <input type=radio name=courseGroup ";
		if($numerrors > 0 && $row['courseCode'] == $courseCode)
		   echo " checked ";
		echo "value=".$row['courseCode']." > ";
		echo " ".$row['courseCode']."</td>";
		echo "<td>".$row['courseName']."</td>";
		echo "<td>".$row['cnt']."</td>";
		echo "</tr>";		
	}
	
?>
</table>

<p>

<table cellpadding="3">
<tr>
<td>
<table border=1 cellpadding="3">
<tr>
<th style="text-align:center">INSTRUCTOR </th>
</tr>
<?php
	$sqlquery = " select instructorID, concat(firstName, ' ', lastName) as iname from instructor ";
	$result = DBConnectionManager::query($sqlquery);
	while($row = mysql_fetch_assoc($result)){
		echo "<tr>";
		echo "<td><input type=radio name=instructorGroup ";
		if($numerrors > 0 && $row['instructorID'] == $instructorID)
		   echo " checked ";
		echo " value=".$row['instructorID']." /> ";
		echo " ".$row['iname']."</td>";

		echo "</tr>";		
	}	
?>
</table>
</td>

<td>
<table cellpadding=3 border=1>
<tr>
<td><b>Day Of Week: </b> </br> <hr>
<input type="radio" name="dayGroup" value="1" <?php if($numerrors > 0 && $lectureDay == 1) echo " checked "; ?> />Monday</br>
<input type="radio" name="dayGroup" value="2" <?php if($numerrors > 0 && $lectureDay == 2) echo " checked "; ?> />Tuesday</br>
<input type="radio" name="dayGroup" value="3" <?php if($numerrors > 0 && $lectureDay == 3) echo " checked "; ?> />Wednesday</br>
<input type="radio" name="dayGroup" value="4" <?php if($numerrors > 0 && $lectureDay == 4) echo " checked "; ?> />Thursday</br>
<input type="radio" name="dayGroup" value="5" <?php if($numerrors > 0 && $lectureDay == 5) echo " checked "; ?> />Friday</br>
<input type="radio" name="dayGroup" value="6" <?php if($numerrors > 0 && $lectureDay == 6) echo " checked "; ?> />Saturday</br>
<input type="radio" name="dayGroup" value="7" <?php if($numerrors > 0 && $lectureDay == 7) echo " checked "; ?> />Sunday</br>
</td>


<td> 
<p>
<b> Start Time(HH:MM): </b> 
<select name="sho" > 
	<option value="08" <?php if($numerrors > 0 && $shour == "08") echo " checked "; ?> >08</option>
	<option value="09" <?php if($numerrors > 0 && $shour == "09") echo " checked "; ?> >09</option>
	<option value="10" <?php if($numerrors > 0 && $shour == "10") echo " checked "; ?> >10</option>
	<option value="11" <?php if($numerrors > 0 && $shour == "11") echo " checked "; ?> >11</option>
	<option value="12" <?php if($numerrors > 0 && $shour == "12") echo " checked "; ?> >12</option>
	<option value="13" <?php if($numerrors > 0 && $shour == "13") echo " checked "; ?> >13</option>
	<option value="14" <?php if($numerrors > 0 && $shour == "14") echo " checked "; ?> >14</option>
	<option value="15" <?php if($numerrors > 0 && $shour == "15") echo " checked "; ?> >15</option>
	<option value="16" <?php if($numerrors > 0 && $shour == "16") echo " checked "; ?> >16</option>
	<option value="17" <?php if($numerrors > 0 && $shour == "17") echo " checked "; ?> >17</option>
	<option value="18" <?php if($numerrors > 0 && $shour == "18") echo " checked "; ?> >18</option>
	<option value="19" <?php if($numerrors > 0 && $shour == "19") echo " checked "; ?> >19</option>
</select>
 : 40 
</p>

<p>
<b> Hours: </b> 
<select name="hours" >  
    <option value="1" <?php if($numerrors > 0 && $hours == 1) echo " selected "; ?> >1</option>
    <option value="2" <?php if($numerrors > 0 && $hours == 2) echo " selected "; ?> >2</option>
    <option value="3" <?php if($numerrors == 0 || $hours == 3) echo " selected "; ?> >3</option>
    <option value="4" <?php if($numerrors > 0 && $hours == 4) echo " selected "; ?> >4</option>
    <option value="5" <?php if($numerrors > 0 && $hours == 5) echo " selected "; ?> >5</option>
    <option value="6" <?php if($numerrors > 0 && $hours == 6) echo " selected "; ?> >6</option>
</select>
</p>
<p><b> Room: </b> <input type="text" 
		maxlength=10 name="room" size="7" value="<?php if($numerrors > 0) echo $room; ?>" 
		 style="text-transform: uppercase" /> </p>
</td>
</tr>

</table>

</td>
<td>
<p>
<input type="submit" value="Submit" onClick="return controlFields()" style="width:100px; height:30px" name="btnSubmit">
</p>  
<p>
<input type="reset" value="Reset Fields" style="width:100px; height:30px" name="btnReset"></p>  
</p>
</td>

</tr>

</table>



</FORM>

<?php include 'bottom.php' ?>

</body>


</html>